<?php /** $Id$ **/
/**
 * A helper for embedding flash into your site using Javascript.
 * This helper is simply a wrapper for the javascript SwfObject vendor found here:
 * 
 *   http://code.google.com/p/swfobject/
 * 
 * It has simply two functions, one is optional. If you wish to embed several
 * flash files into your view, you can initialize the helper (include the javascript
 * library) once and also set default options for all your subsequent calls.
 *
 * Example 1 :
 * 
 * echo $flash->renderSwf('test.swf',400,200,'flashy');
 * echo '<div id="flashy"></div>';
 * 
 * Example 2 :
 * 
 * $flash->init(array('width'=>200,'height'=>100));
 * echo $flash->renderSwf('test1.swf');
 * echo $flash->renderSwf('test12swf');
 * 
 * Example 3 :
 * 
 * echo $flash->renderSwf('fl_countdown_v3_3.swf?mo=1&da=24&snd=off&co=AA3333',800,250,false,array('params' => array('movie'=>'?mo=1&da=24&snd=off&co=AA3333')));
 * 
 * @author Alexander Morland
 * @license MIT
 * @version 1.0
 * @modified 13. nov. 2008
 */
class FlashHelper extends AppHelper {	
	var $helpers = array('Javascript');
/**
 * Used for remembering options from init() to each renderSwf
 *
 * @var array
 */
	var $options = array(
					'width' => 100,
					'height' => 100
				);

/**
 * Used by renderSwf to only call init if it hasnt been done, either
 * manually or automatically by a former renderSwf()
 *
 * @var boolean
 */
	var $initialized = false;
 
/**
 * Optional initializing for setting default parameters and also includes the
 * swf library. Should be called once, but if using several groups of flashes,
 * MAY be called several times, once before each group.
 *
 * @example echo $flash->init();
 * @example $flash->init(array('width'=>200,'height'=>100);
 * @return mixed String if it was not able to add the script to the view, true if it was
 */
	function init($options = array()) {
		if (!empty($options)) {
			$this->options = am($this->options, $options);
		}
		$this->initialized = TRUE;
		$view = ClassRegistry::getObject('view'); 
		if (is_object($view)) { 
			$view->addScript($this->Javascript->link('swfobject')); 
			return true;
		} else {
			return $this->Javascript->link('swfobjec');
        }
    }
	   
/**
 * The main feature of the helper, which is called to render a specific flash (swf) file.
 * First three parameters should be self explanatory. The $divDomId when set to false will
 * have the helper generate a div with a unique id that the swfobject will replace. If
 * given a string, it will replace the div with the domId of the string given. The third
 *
 * @param string $swfFile
 * @param int $width if null, will use width set by FlashHelper::init()
 * @param int $height if null, will use height set by FlashHelper::init()
 * @param mixed $divDomId false or string : dom id
 * @param array $options array('flashvars'=>array(),'params'=>array('wmode'=>'opaque'),'attributes'=>array());
 *         See SwfObject documentation for valid options
 * @return string
 */
	function renderSwf($swfFile, $width = null, $height = null, $divDomId = false, $options = array()) {
		$options = am ($this->options, $options);        
		if (is_null($width)) {
			$width = $options['width'];
		}
		if (is_null($height)) {
			$height = $options['height'];
        }
		$ret = '';
		if (!$this->initialized) {
			$init = $this->init($options);
            if (is_string($init)) {
				$ret = $init;
	        }
            $this->initialized = TRUE;
        }
        if (empty($options)) {            
            $extra = array(
             'flashvars' => '{}',
             'params' => '{wmode : "opaque"}',
             'attributes' => '{}',
           );
       } else {            
            $extra = array();
			foreach (array('flashvars','params','attributes') as $type) {
				if (!empty($options[$type])) {
					$arr = array();
                    foreach($options[$type] as $key=>$val) {
						$arr[] = "$key:\"$val\"";
					}
					$extra[$type] = '{'.implode(', ', $arr).'}';
				 } else {
					 $extra[$type] = '{}';
				 }
		   }
		}

        if ($divDomId === false) {
			$divDomId = uniqid('c_');
            $ret .= '<div id="'.$divDomId.'"></div>';
        }
		$version = (isset($options['version'])) ? $options['version'] : '10.0.0';
		$install = (isset($options['install'])) ? $options['install'] : '';
        $swfLocation = $this->base.'/'.$swfFile;
		$ret .= $this->Javascript->codeBlock(
		            'swfobject.embedSWF("'.$swfLocation.'", "'.$divDomId.'", "'.$width.'", "'.$height.'", "'.$version.'","'.$install.'", '.$extra['flashvars'].', '.$extra['params'].', '.$extra['attributes'].');');
							    
		return $ret;
    }
}
?>
